from django.db import models
from django.contrib.auth.models import User
import datetime
from pprint import pprint
#from helper import calculate_rank

# This model isn't as clean as I'd like, but will do for now
class Semester(models.Model):
        year = models.DateField() # just use the year
        season = models.IntegerField()  #limit on top (Spring, Summer, Fall)

# We could split the course into more detail, 
# so the teacher won't have to reregister info for each exam.
# But we might not need to for this project
# todo: check if we have to enforce any student is taking course rules. I don't think so, but just need to make sure.
class Course(models.Model):
        name = models.CharField(max_length = 64)
        teacher = models.ForeignKey(User)
        semester = models.ForeignKey(Semester)
        
        def __unicode__(self):
            return self.name + " " + self.semester

class Exam(models.Model):
	name = models.CharField(max_length=64)
	#course = models.ForeignKey(Course)        
	date_created = models.DateField()	
	deadline = models.DateField()

	def __unicode__(self):
	    return self.name

	def created_today(self):
	    return self.date_created == datetime.date.today()
	created_today.short_description = "Was created today?"

# I think we should just have the view inforce at least 1 correct answer
# I'm not sure the model can handle the db solutions I can think of
class Question(models.Model):
	exam = models.ForeignKey(Exam)
	text = models.CharField(max_length=256)
	number = models.IntegerField(null=True) # for ordering
	value = models.IntegerField(null=True) # for grading

	def __unicode__(self):
            return self.text

class Answer(models.Model):
        question = models.ForeignKey(Question)
        text = models.CharField(max_length=128)
        number = models.IntegerField(null=True)
        is_correct = models.BooleanField()

	def __unicode__(self):
            return self.text

class TakenExam(models.Model):
	exam = models.ForeignKey(Exam)
	student = models.ForeignKey(User)
	dateTaken = models.DateField()
	grade = models.FloatField() 

	class Meta:
		unique_together = ('exam', 'student',)

	def __unicode__(self):
		return self.exam.name

        def get_rank(self):
            taken_exams = TakenExam.objects.filter(exam=self.exam)
            higher = 0
            same = 0
            for exam in taken_exams:
                if self.grade == exam.grade:
                    same = same + 1
                elif self.grade < exam.grade:
                    higher = higher + 1
            total = len(taken_exams)
            rank = str(higher + 1)
            if same > 1: # self is included
                rank = rank + '-' + str(higher + same)
            pprint(total)
            return rank + '/' + str(total)
        
class Selection(models.Model):
        exam = models.ForeignKey(TakenExam)
        question = models.ForeignKey(Question)
        answer = models.ForeignKey(Answer)

	def __unicode__(self):
            return self.answer.text



